<?php
use app\model\Users;
use think\Request;

//use think\facade\Db;
/* 应用公共文件 */
//返回成功
function success($data=null,$message='ok')
{
    $parm = [
        'code' => 200,
        'message' => $message,
        'data' => $data,
    ];
    return json($parm);
}
function successPage($data=null)
{
    $return =json_encode($data);
    $return =json_decode($return);
    $return->code =200;
    $return->message ='ok';
    return json($return);
}
//返回失败
function fail($data, string $message = 'error', int $code = -1)
{
    $parm = [
        'code' => $code,
        'message' => $message,
        'data' => $data,
    ];
    return json($parm);
}
//返回分页
function pageReturn($data)
{
    if($data){
        $data = $data->toArray();
        return json([
            'code' => 200,
            'message' => 'ok',
            'total' => $data['total'],
            'pageSize' => $data['per_page'],
            'current' => $data['current_page'],
            'last_page' => $data['last_page'],
            'data' => $data['data'],
        ]);
    }else{
        return json([
            'code' => 200,
            'message' => 'ok',
            'data' => [],
        ]);
    }


}
//用户信息
function auth_user(Request $request)
{
    $user = Users::where('token', $request->header('authorization'))->find();
    return $user;
}
